Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Baggage and Tags for activity tracking options (#46571) #48722

Merged

Conversation

msallin
Copy link
Contributor

@msallin msallin commented Feb 24, 2021

@tarekgh here we go. It's the simplest implementation I can think of.
As stated in #47315 a logging scope is expected to be IEnumerable<KeyValuePair<string, object>> *
Tags and baggage are already IEnumerable<KeyValuePair<string, object>> and can be used.
It makes no sense to cache them like it's done for the ActivityLogScope, as they change from one log line to the other.

Tests will follow.

* Is there a reason the ActivityLogScope implements IReadOnlyList<T>?

@dotnet-issue-labeler
Copy link

Note regarding the new-api-needs-documentation label:

This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, to please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change.

@ghost
Copy link

ghost commented Feb 24, 2021

Tagging subscribers to this area: @maryamariyan
See info in area-owners.md if you want to be subscribed.

Issue Details

@tarekgh here we go. It's the simplest implementation I can think of.
As stated in #47315 a logging scope is expected to be IEnumerable<KeyValuePair<string, object>> *
Tags and baggage are already IEnumerable<KeyValuePair<string, object>> and can be used.
It makes no sense to cache them like it done for the ActivityLogScope, as they change from one log line to the other.

Tests will follow.

* Is there a reason the ActivityLogScope implements IReadOnlyList<T>?

Author: msallin
Assignees: -
Labels:

area-Extensions-Logging, new-api-needs-documentation

Milestone: -

@dnfadmin
Copy link

dnfadmin commented Feb 24, 2021

CLA assistant check
All CLA requirements met.

@msallin msallin changed the title Topic/#46571/addition for activity tracking options Baggage and Tags for activity tracking options (#46571) Feb 24, 2021
@tarekgh
Copy link
Member

tarekgh commented Feb 24, 2021

Also, could you please add tests for this added functionality?

public void TestActivityIds(ActivityTrackingOptions options)

@tarekgh
Copy link
Member

tarekgh commented Feb 24, 2021

CC @davidfowl

@tarekgh
Copy link
Member

tarekgh commented Feb 25, 2021

Is there a reason the ActivityLogScope implements IReadOnlyList?

I don't think it has to implement IReadOnlyList<T> and it can be just IEnumerable<KeyValuePair<string, object>>. I believe originally implemneted with IReadOnlyList<T> because JsonConsoleFormatter used to check explicitly for IReadOnlyList<T> but we changed that recently to check for IEnumerable<KeyValuePair<string, object>>.

@msallin
Copy link
Contributor Author

msallin commented Feb 25, 2021

Also, could you please add tests for this added functionality?

public void TestActivityIds(ActivityTrackingOptions options)

Of course. Its on my TODO-list.

Is there a reason the ActivityLogScope implements IReadOnlyList?

I don't think it has to implement IReadOnlyList<T> and it can be just IEnumerable<KeyValuePair<string, object>>. I believe originally implemneted with IReadOnlyList<T> because JsonConsoleFormatter used to check explicitly for IReadOnlyList<T> but we changed that recently to check for IEnumerable<KeyValuePair<string, object>>.

Okay. Should I change from IReadyOnlyList<T> to IEnumerable<T>? Note: This is potentially breaking because one could use GetCustomProperty in his code to get the the IReadOnlyList<T> by using the name __ActivityLogScope__.

@tarekgh
Copy link
Member

tarekgh commented Feb 25, 2021

Also, could you please add tests for this added functionality?

Of course. Its on my TODO-list.

Thank you.

Should I change from IReadyOnlyList to IEnumerable?

Not important to do so for now.

Base automatically changed from master to main March 1, 2021 09:08
@tarekgh
Copy link
Member

tarekgh commented Mar 1, 2021

@msallin did you have a chance to address the remaining feedback so we can move on?

@msallin msallin force-pushed the topic/#46571/AdditionForActivityTrackingOptions branch from 5eb3df4 to 58b9152 Compare March 1, 2021 22:39
@msallin
Copy link
Contributor Author

msallin commented Mar 1, 2021

@msallin did you have a chance to address the remaining feedback so we can move on?

Wasn't able yet do run the unit tests locally and I'm sure they fail.
I will fix them tomorrow evening and mark the PR as ready for review as soon as I'm ready.

@tarekgh
Copy link
Member

tarekgh commented Mar 1, 2021

Wasn't able yet do run the unit tests locally and I'm sure they fail.

Let me know if you need help with that.

@msallin msallin force-pushed the topic/#46571/AdditionForActivityTrackingOptions branch from 58b9152 to 83b67c4 Compare March 2, 2021 18:23
@tarekgh
Copy link
Member

tarekgh commented Mar 2, 2021

@msallin looks there is some errors https://helixre8s23ayyeko0k025g8.blob.core.windows.net/dotnet-runtime-refs-pull-48722-merge-5b4b06b341d4447e9f/Microsoft.Extensions.Logging.Tests/console.edeb4dfd.log?sv=2019-07-07&se=2021-03-22T19%3A14%3A10Z&sr=c&sp=rl&sig=dUgpUHJSMEj0keRHUAiXUeIVIrGKg4F81mABpiBDnEA%3D

Looks you missed updating the line

const ActivityTrackingOptions ActivityTrackingOptionsMask = ~(ActivityTrackingOptions.SpanId | ActivityTrackingOptions.TraceId | ActivityTrackingOptions.ParentId |

[19:54:26] info: Discovering: Microsoft.Extensions.Logging.Tests.dll (method display = ClassAndMethod, method display options = None)
[19:54:26] info: Discovered:  Microsoft.Extensions.Logging.Tests.dll (found 104 of 127 test cases)
[19:54:26] info: Starting:    Microsoft.Extensions.Logging.Tests.dll
[19:54:26] fail: [FAIL] Microsoft.Extensions.Logging.Test.LoggerFactoryTest.TestActivityTrackingOptionsTags
[19:54:26] info: System.ArgumentException : Tags is invalid ActivityTrackingOptions value. (Parameter 'options')
[19:54:26] info:    at Microsoft.Extensions.Logging.LoggerFactory..ctor(IEnumerable`1 providers, IOptionsMonitor`1 filterOption, IOptions`1 options)
[19:54:26] info:    at System.Reflection.RuntimeConstructorInfo.InternalInvoke(Object obj, Object[] parameters, Boolean wrapExceptions)
[19:54:26] info:    at System.Reflection.RuntimeConstructorInfo.DoInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
[19:54:26] info:    at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
[19:54:26] info:    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)
[19:54:26] info:    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2[[Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext, Microsoft.Extensions.DependencyInjection, Version=6.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60],[System.Object, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].VisitCallSiteMain(ServiceCallSite callSite, RuntimeResolverContext argument)

@msallin msallin force-pushed the topic/#46571/AdditionForActivityTrackingOptions branch from 83b67c4 to 703a705 Compare March 2, 2021 21:02
@msallin
Copy link
Contributor Author

msallin commented Mar 2, 2021

Here we go. Now I've something we can work with/discuss.

  1. My current approach doesn't work because a Scope needs to support ToString()

This can be seen by looking at the test:

_scopeProvider.ForEachScope((scope, builder) => builder.Add(scope.ToString()), LogText);

Or in the SimpleConsoleFormatter:

The JsonConsoleFormatter e.g. would behave correctly:

foreach (KeyValuePair<string, object> item in scopes)

Either the SimpleConsoleFormatter needs to be adjusted to iterate and not call ToString() or we need to wrap the Tags & Baggages in an object and overwrite ToString().
Third option is to overwrite ToString() for the BaggageLinkedList and `TagsLinkedList"

private TagsLinkedList? _tags;
private BaggageLinkedList? _baggage;

  1. Some things to define

What do we do if tag or baggage value is null?
I don't know if it makes sense to log smth like "myKey: "

What do we do if the key of a baggage or tag is empty (is it even possible)?
I need to check if one can even add this to an Activity.

@tarekgh
Copy link
Member

tarekgh commented Mar 2, 2021

Either the SimpleConsoleFormatter needs to be adjusted to iterate and not call ToString() or we need to wrap the Tags & Baggages in an object and overwrite ToString().
Third option is to overwrite ToString() for the BaggageLinkedList and `TagsLinkedList"

The other option which I am inclining to is to add the Tags and Baggage to ActivityLogScope and then format them in ToString there. We'll still always get the Tag and Baggage from the Activity object to ensure using the updated lists there.

What do we do if tag or baggage value is null?

I believe it is ok to produce empty string at that time.

What do we do if the key of a baggage or tag is empty (is it even possible)? I need to check if one can even add this to an Activity.

It is possible, at that time we'll produce empty string for the key.

As I suggested, if we move the formatting logic to ActivityLogScope we can easily control the behavior.

@msallin
Copy link
Contributor Author

msallin commented Mar 2, 2021

The other option which I am inclining to is to add the Tags and Baggage to ActivityLogScope and then format them in ToString there. We'll still always get the Tag and Baggage from the Activity object to ensure using the updated lists there.

We have the activity as ctor param. You mean I should create a field and hold the Activity in the ActivityLogScope?
As a consquence the current baggage and tags can be applied in ToString(). The GetEnumerator will yield return _activity.TagObjects and baggage. But what about the access by Index and count?

@tarekgh
Copy link
Member

tarekgh commented Mar 9, 2021

Regarding the debugger output of activity accessing, it looks it is using the activity you have created which make sense. But still, something unclear to me.

Execute AdditionalActivityItemsLogScope ScopeActivity(|efff3b4b-4661424b2ed3102b.)
Instance was null ScopeActivity(|efff3b4b-4661424b2ed3102b.)

These two lines means the object AdditionalActivityItemsLogScope is created using the value activity.Baggage and then cached inside the activity object. then the following lines:

Execute AdditionalActivityItemsLogScope ScopeActivity(|efff3b4b-4661424b2ed3102b.)
Execute AdditionalActivityItemsLogScope ScopeActivity(|efff3b4b-4661424b2ed3102b.)
Execute AdditionalActivityItemsLogScope ScopeActivity(|efff3b4b-4661424b2ed3102b.)

means the cached value of AdditionalActivityItemsLogScope is retrieved and used in the logging. But this value is still holding the original IEnumerable value returned from activity.Baggage and that value shouldn't be including the other added key-values since we cached this object. The question, how the logs got the other key-value pairs added to the activity.Baggage while we used the old-cached value? Looks I am missing something in the middle here.

@msallin
Copy link
Contributor Author

msallin commented Mar 9, 2021

We don't cache the values. We always reenumerate the IEnumerable. This would cache the values:
additionalActivityItemsLogScope = new AdditionalActivityItemsLogScope<TValue>(items.ToList());

@tarekgh
Copy link
Member

tarekgh commented Mar 9, 2021

We don't cache the values. We always reenumerate the IEnumerable. This would cache the values:

You are right. I was assuming Activity.Baggage internally returning a list but it looks not.

@tarekgh
Copy link
Member

tarekgh commented Mar 9, 2021

@msallin please let me know when you address the remaining comments and we'll be good to go. thanks!

@msallin
Copy link
Contributor Author

msallin commented Mar 10, 2021

@tarekgh I think there aren't any comments left.

@msallin msallin marked this pull request as ready for review March 10, 2021 22:52
@tarekgh
Copy link
Member

tarekgh commented Mar 10, 2021

@msallin sorry it was my fault that I wrote the comments and didn't submit it. I posted them now. Thanks!

@msallin
Copy link
Contributor Author

msallin commented Mar 10, 2021

@msallin sorry it was my fault that I wrote the comments and didn't submit it. I posted them now. Thanks!

Great! Ty! Consider them done until the end of this week.

@msallin msallin force-pushed the topic/#46571/AdditionForActivityTrackingOptions branch 2 times, most recently from 315c462 to 0e98759 Compare March 11, 2021 08:43
@msallin
Copy link
Contributor Author

msallin commented Mar 11, 2021

@tarekgh I addressed all the comments. You might have a look again at the PR again and let me know if there is something I should rework.

@tarekgh
Copy link
Member

tarekgh commented Mar 11, 2021

@msallin looks good to me. I have added one last minor comment and we'll be good to merge.

@tarekgh tarekgh self-assigned this Mar 11, 2021
@msallin msallin force-pushed the topic/#46571/AdditionForActivityTrackingOptions branch from 0e98759 to 11b8166 Compare March 11, 2021 18:42
@tarekgh
Copy link
Member

tarekgh commented Mar 11, 2021

@msallin

##[error]src/libraries/Microsoft.Extensions.Logging/src/LoggerFactoryScopeProvider.cs(62,93): error SA1028: (NETCORE_ENGINEERING_TELEMETRY=Build) Code should not contain trailing whitespace

@msallin msallin force-pushed the topic/#46571/AdditionForActivityTrackingOptions branch from 11b8166 to 7e64b80 Compare March 12, 2021 09:44
@tarekgh tarekgh merged commit dacf02b into dotnet:main Mar 12, 2021
@tarekgh
Copy link
Member

tarekgh commented Mar 12, 2021

@msallin thanks a lot for helping with this issue.

@msallin msallin deleted the topic/#46571/AdditionForActivityTrackingOptions branch March 12, 2021 17:45
@ghost ghost locked as resolved and limited conversation to collaborators Apr 11, 2021
@karelz karelz added this to the 6.0.0 milestone May 20, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants